import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:xin_he/page/mine/xin_he_adress_management/model/xin_he_city_model.dart';
import 'package:xin_he/utils/xh_styles.dart';
import 'package:xin_he/page/mine/xin_he_adress_management/model/xin_he_p_c_a_model_entity.dart';

//省市区类型
enum CityType {
  province,
  city,
  area
}

typedef SureCallback  = Function(XinHeCityModel province,XinHeCityModel city,XinHeCityModel aera);
typedef CancelCallback = Function();
class XinHeCityPicker extends StatefulWidget {
   XinHeCityPicker({Key? key,required this.list, required this.sureCallback, required this.cancelCallback}) : super(key: key);
   final List<XinHePCAModelEntity> list;
   final SureCallback sureCallback;
   final CancelCallback cancelCallback;


   @override
  State<XinHeCityPicker> createState() => _XinHeCityPickerState();
}

class _XinHeCityPickerState extends State<XinHeCityPicker> {

  List <XinHeCityModel> _provinceList = [];

  List <XinHeCityModel> _cityList = [];

  List <XinHeCityModel> _areaList = [];
  GlobalKey _provinceGlobalKey = GlobalKey();

  GlobalKey _cityGlobalKey = GlobalKey();

  GlobalKey _areaGlobalKey = GlobalKey();

  int _provinceIndex = 0;

  int _cityIndex = 0;

  int _areaIndex = 0;


  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    List<XinHeCityModel> tmpList = [];
    for(XinHePCAModelEntity e in widget.list){
      XinHeCityModel model = XinHeCityModel();
      model.id = e.id;
      model.name = e.name;
      model.extName = e.extName;
      tmpList.add(model);
    }
    _provinceList = tmpList;


    XinHePCAModelEntity entity = widget.list[_provinceIndex];
    List<XinHeCityModel> tmpCitiesList = [];
    for(XinHePCAModelChildren e in entity.children!){
      XinHeCityModel model = XinHeCityModel();
      model.id = e.id;
      model.name = e.name;
      model.extName = e.extName;
      tmpCitiesList.add(model);
    }

    _cityList = tmpCitiesList;
    List<XinHeCityModel> tmpAeraList = [];
    for(XinHePCAModelChildrenChildren e in  entity.children![_cityIndex].children!){
      XinHeCityModel model = XinHeCityModel();
      model.id = e.id;
      model.name = e.name;
      model.extName = e.extName;
      tmpAeraList.add(model);
    }
    _areaList = tmpAeraList;
    setState(() {});


  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(child: Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        const SizedBox(height: 8,),
        _headerWidget(context),
        Row(
          children: [

            _pickerViewWidget(
                models:_provinceList,
                key:_provinceGlobalKey ,
                onSelectedItemChanged: (v){


                  setState(() {
                    _provinceIndex = v;
                    _cityIndex = 0;
                    _areaIndex = 0;

                    XinHePCAModelEntity entity = widget.list[v];
                    List<XinHeCityModel> tmpCitiesList = [];
                    for(XinHePCAModelChildren e in entity.children!){
                      XinHeCityModel model = XinHeCityModel();
                      model.id = e.id;
                      model.name = e.name;
                      model.extName = e.extName;
                      tmpCitiesList.add(model);
                    }

                    _cityList = tmpCitiesList;


                    List<XinHeCityModel> tmpAeraList = [];
                    for(XinHePCAModelChildrenChildren e in  entity.children![_cityIndex].children!){
                      XinHeCityModel model = XinHeCityModel();
                      model.id = e.id;
                      model.name = e.name;
                      model.extName = e.extName;
                      tmpAeraList.add(model);
                    }
                    _areaList = tmpAeraList;
                  });

                },
                type: CityType.province
            ),

            _pickerViewWidget(
                models: _cityList,
                key:_cityGlobalKey,
                onSelectedItemChanged: (v){



              setState(() {
                _cityIndex = v;
                _areaIndex = 0;

                XinHePCAModelChildren cityModel = widget.list[_provinceIndex].children![v];
                List<XinHeCityModel> tmpAeraList = [];
                for(XinHePCAModelChildrenChildren e in  cityModel.children!){
                  XinHeCityModel model = XinHeCityModel();
                  model.id = e.id;
                  model.name = e.name;
                  model.extName = e.extName;
                  tmpAeraList.add(model);
                }
                _areaList = tmpAeraList;
              });
            },
                type: CityType.city
            ),

            _pickerViewWidget(
                models: _areaList,
                key:_areaGlobalKey,
                onSelectedItemChanged: (v){
                  setState(() {
                _areaIndex =v;

              }
                  );
                },
                type: CityType.area
                ),
          ],
        )
      ],
    ));
  }

  Widget _headerWidget(BuildContext context){
    return Row(
      children: [
        _buttonWidget(title: '取消',textColor: Colors.black38,callback: (){
          // _canlClick(context);
          widget.cancelCallback();
        }),
        Expanded(child: Container()),
        _buttonWidget(title: '确定',textColor: Colors.black,callback: (){
          // _confirmClick(context);
          widget.sureCallback(_provinceList[_provinceIndex],_cityList[_cityIndex], _areaList[_areaIndex]);
        }),
      ],
    );
  }

//piceerView
  Widget _pickerViewWidget({List<XinHeCityModel>? models,Key? key, ValueChanged<int>? onSelectedItemChanged,CityType? type}){
    return Expanded(
        child: SizedBox(
            height: 200,
            child: NotificationListener(
              onNotification: (Notification scrollNotification) {
                if (scrollNotification is ScrollEndNotification &&
                    scrollNotification.metrics is FixedExtentMetrics)
                {
                  print('noti =${(scrollNotification.metrics as FixedExtentMetrics).itemIndex}'); // Index of the list

                  // if(type !=null  ){
                  //   if(type == CityType.province){
                  //     setState(() {
                  //       _cityIndex = 0;
                  //       _areaIndex = 0;
                  //     });
                  //   }
                  //   if(type == CityType.city){
                  //     _areaIndex = 0;
                  //   }
                  //   if(type == CityType.province){
                  //
                  //   }
                  // }
                  onSelectedItemChanged!((scrollNotification.metrics as FixedExtentMetrics).itemIndex);

                  return true;
                } else {
                  return false;
                }
              },
              child: CupertinoPicker(
                  key: key,
                  useMagnifier: true,
                  magnification: 1.2,
                  selectionOverlay: _selectionOverlayWidget(),
                  itemExtent: 34,
                  onSelectedItemChanged: (v){
                  },
                  children: models!.map((e) => _itemsWidget(e.name)).toList()),
            ))
    );
  }

// 中间分割线
  Widget _selectionOverlayWidget(){
    return Padding(
      padding: EdgeInsets.only(left: 0, right: 0),
      child: Column(
        children: [
          Divider(
            height: 1,
            color: XinHeStyle.lineColor,
          ),
          Expanded(child: Container()),
          Divider(
            height: 1,
            color:  XinHeStyle.lineColor,
          ),
        ],
      ),
    );
  }

// cellItems
  Widget _itemsWidget(e){
    return Container(
      alignment: Alignment.center,
      child:  Text(e,style: TextStyle(fontSize: 14),),
    );
  }

  //公共button
  Widget _buttonWidget({String? title  ,Color? textColor,VoidCallback? callback}){
    return  InkWell(
      onTap: callback,
      child: Container(
        alignment: Alignment.center,
        padding:EdgeInsets.only(left: 16,right: 16),
        height: 40,
        child: Text(title??'',style: TextStyle(fontSize: 14),),
      ),
    );
  }

}




